/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.config;

import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;

import javax.sql.DataSource;


/**
 * Created by Peter on 2016/8/15.
 */
@Configuration
public class DataSourceConfig {
    /**
     * Declared the datasource for application.
     * For Profile development, production
     *
     * @return DataSource
     */
    @Bean(name = "dataSource")
    @Profile({Profiles.DEVELOPMENT, Profiles.PRODUCTION})
    public DataSource firstDataSource() {
        SingleConnectionDataSource ds = (SingleConnectionDataSource) DataSourceBuilder
                .create()
                .type(org.springframework.jdbc.datasource.SingleConnectionDataSource.class)
                .url("jdbc:sqlite:data/ispc.s3db")
                .username("ispc")
                .password("ispc")
                .driverClassName("org.sqlite.JDBC")
                .build();
        ds.setSuppressClose(true);
        return ds;
    }

    /**
     * Declared the datasource for application.
     * For Profile test
     *
     * @return DataSource
     */
    @Bean(name = "dataSource")
    @Profile(Profiles.TEST)
    public DataSource testDataSource() {
        SingleConnectionDataSource ds = (SingleConnectionDataSource) DataSourceBuilder
                .create()
                .type(org.springframework.jdbc.datasource.SingleConnectionDataSource.class)
                .url("jdbc:sqlite:../data/ispc-test.s3db")
                .username("ispc")
                .password("ispc")
                .driverClassName("org.sqlite.JDBC")
                .build();
        ds.setSuppressClose(true);
        return ds;
    }
}
